using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Unity.VisualScripting;

public class ChatController : NetworkBehaviour
{
    public TMP_InputField chatText;//输入框
    public Button chatBtn;//发送按钮
    public GameObject chatInfo;//聊天框内容预制体
    public GameObject chatFrame;//聊天框
    public Player player;

    [SyncVar(hook = nameof(OnChatTextStringChanged))]
    public string chatTextString;

    private void OnChatTextStringChanged(string oldstr, string newstr)
    {
        Debug.Log(444);
        GameObject ci = Instantiate(chatInfo);
        ci.GetComponent<TMP_Text>().text = newstr;
        ci.transform.SetParent(chatFrame.transform);
    }

    void Awake()
    {
        Debug.Log(111);
        chatBtn.onClick.AddListener(SendBtn);
    }

    public void SendBtn()
    {
        if (player != null)
        {
            Debug.Log(222);
            player.CmdSendPLayerMessage(chatText.text);
        }
    }

    // void Update()
    // {
    //     if (Input.GetKeyDown(KeyCode.T))
    //     {
    //         NetworkServer.Destroy(player.gameObject);
    //         // 创建一个新的Camera对象
    //         GameObject cameraObject = new GameObject("Main Camera");
    //         // 添加Camera组件到对象上
    //         Camera cameraComponent = cameraObject.AddComponent<Camera>();
    //         // 设置摄像机的位置和旋转
    //         cameraComponent.transform.position = new Vector3(0, 0, -10f);
    //         cameraComponent.transform.rotation = Quaternion.identity;
    //     }
    // }

}
